<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BOOST_FUSION_ADAPT_ADT</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Fusion 2.2">
<link rel="up" href="../adapted.html" title="Adapted">
<link rel="prev" href="adapt_assoc_struct_named.html" title="BOOST_FUSION_ADAPT_ASSOC_STRUCT_NAMED">
<link rel="next" href="adapt_tpl_adt.html" title="BOOST_FUSION_ADAPT_TPL_ADT">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="adapt_assoc_struct_named.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../adapted.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="adapt_tpl_adt.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="fusion.adapted.adapt_adt"></a><a class="link" href="adapt_adt.html" title="BOOST_FUSION_ADAPT_ADT">BOOST_FUSION_ADAPT_ADT</a>
</h3></div></div></div>
<p>
        BOOST_FUSION_ADAPT_ADT is a macro than can be used to generate all the necessary
        boilerplate to adapt an arbitrary class type as a model of <a class="link" href="../sequence/concepts/random_access_sequence.html" title="Random Access Sequence">Random
        Access Sequence</a>.
      </p>
<h5>
<a name="fusion.adapted.adapt_adt.h0"></a>
        <span class="phrase"><a name="fusion.adapted.adapt_adt.synopsis"></a></span><a class="link" href="adapt_adt.html#fusion.adapted.adapt_adt.synopsis">Synopsis</a>
      </h5>
<pre class="programlisting"><span class="identifier">BOOST_FUSION_ADAPT_ADT</span><span class="special">(</span>
    <span class="identifier">type_name</span><span class="special">,</span>
    <span class="special">([</span><span class="identifier">attribute_type0</span><span class="special">,</span> <span class="identifier">attribute_const_type0</span><span class="special">,]</span> <span class="identifier">get_expr0</span><span class="special">,</span> <span class="identifier">set_expr0</span><span class="special">)</span>
    <span class="special">([</span><span class="identifier">attribute_type1</span><span class="special">,</span> <span class="identifier">attribute_const_type1</span><span class="special">,]</span> <span class="identifier">get_expr1</span><span class="special">,</span> <span class="identifier">set_expr1</span><span class="special">)</span>
    <span class="special">...</span>
    <span class="special">)</span>
</pre>
<h5>
<a name="fusion.adapted.adapt_adt.h1"></a>
        <span class="phrase"><a name="fusion.adapted.adapt_adt.expression_semantics"></a></span><a class="link" href="adapt_adt.html#fusion.adapted.adapt_adt.expression_semantics">Expression
        Semantics</a>
      </h5>
<p>
        The above macro generates the necessary code to adapt <code class="computeroutput"><span class="identifier">type_name</span></code>
        as a model of <a class="link" href="../sequence/concepts/random_access_sequence.html" title="Random Access Sequence">Random
        Access Sequence</a>. The sequence of <code class="literal">([attribute_type<span class="emphasis"><em>N</em></span>,
        attribute_const_type<span class="emphasis"><em>N</em></span>,</code> get_expr<span class="emphasis"><em>N</em></span>,
        set_expr<span class="emphasis"><em>N</em></span>)] quadruples declares the types, const types,
        get-expressions and set-expressions of the elements that are part of the
        adapted fusion sequence. <code class="literal">get_expr<span class="emphasis"><em>N</em></span></code>
        is the expression that is invoked to get the <span class="emphasis"><em>N</em></span>th element
        of an instance of <code class="computeroutput"><span class="identifier">type_name</span></code>.
        This expression may access a variable named <code class="computeroutput"><span class="identifier">obj</span></code>
        of type <code class="computeroutput"><span class="identifier">type_name</span><span class="special">&amp;</span></code>
        or <code class="computeroutput"><span class="identifier">type_name</span> <span class="keyword">const</span><span class="special">&amp;</span></code> which represents the underlying instance
        of <code class="computeroutput"><span class="identifier">type_name</span></code>. <code class="literal">attribute_type<span class="emphasis"><em>N</em></span></code>
        and <code class="literal">attribute_const_type<span class="emphasis"><em>N</em></span></code> may specify
        the types that <code class="literal">get_expr<span class="emphasis"><em>N</em></span></code> denotes
        to, when omitted the type is deduced from [get_expr<span class="emphasis"><em>N</em></span>]
        return type via BOOST_TYPEOF. On compiler missing support for variadic macros
        auto can be used to avoid repeating the type. <code class="literal">set_expr<span class="emphasis"><em>N</em></span></code>
        is the expression that is invoked to set the <span class="emphasis"><em>N</em></span>th element
        of an instance of <code class="computeroutput"><span class="identifier">type_name</span></code>.
        This expression may access variables named <code class="computeroutput"><span class="identifier">obj</span></code>
        of type <code class="computeroutput"><span class="identifier">type_name</span><span class="special">&amp;</span></code>,
        which represent the corresponding instance of <code class="computeroutput"><span class="identifier">type_name</span></code>,
        and <code class="computeroutput"><span class="identifier">val</span></code> of an arbitrary const-qualified
        reference template type parameter <code class="computeroutput"><span class="identifier">Val</span></code>,
        which represents the right operand of the assignment expression.
      </p>
<p>
        The actual return type of fusion's intrinsic sequence access (meta-)functions
        when in invoked with (an instance of) <code class="computeroutput"><span class="identifier">type_name</span></code>
        is a proxy type. This type is implicitly convertible to the attribute type
        via <code class="literal">get_expr<span class="emphasis"><em>N</em></span></code> and forwards assignment
        to the underlying element via <code class="literal">set_expr<span class="emphasis"><em>N</em></span></code>.
        The value type (that is the type returned by <a class="link" href="../iterator/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">value_of</span></code></a>, <a class="link" href="../sequence/intrinsic/metafunctions/value_at.html" title="value_at"><code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">value_at</span></code></a> and <a class="link" href="../sequence/intrinsic/metafunctions/value_at_c.html" title="value_at_c"><code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">value_at_c</span></code></a>) of the <span class="emphasis"><em>N</em></span>th
        element is <code class="literal">attribute_type<span class="emphasis"><em>N</em></span></code> with const-qualifier
        and reference removed.
      </p>
<p>
        The macro should be used at global scope, and <code class="computeroutput"><span class="identifier">type_name</span></code>
        should be the fully namespace qualified name of the class type to be adapted.
      </p>
<h5>
<a name="fusion.adapted.adapt_adt.h2"></a>
        <span class="phrase"><a name="fusion.adapted.adapt_adt.header"></a></span><a class="link" href="adapt_adt.html#fusion.adapted.adapt_adt.header">Header</a>
      </h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">adapted</span><span class="special">/</span><span class="identifier">adt</span><span class="special">/</span><span class="identifier">adapt_adt</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">adapt_adt</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<h5>
<a name="fusion.adapted.adapt_adt.h3"></a>
        <span class="phrase"><a name="fusion.adapted.adapt_adt.example"></a></span><a class="link" href="adapt_adt.html#fusion.adapted.adapt_adt.example">Example</a>
      </h5>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">demo</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">employee</span>
    <span class="special">{</span>
    <span class="keyword">private</span><span class="special">:</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">name</span><span class="special">;</span>
        <span class="keyword">int</span> <span class="identifier">age</span><span class="special">;</span>

    <span class="keyword">public</span><span class="special">:</span>
        <span class="keyword">void</span> <span class="identifier">set_name</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">n</span><span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">name</span><span class="special">=</span><span class="identifier">n</span><span class="special">;</span>
        <span class="special">}</span>

        <span class="keyword">void</span> <span class="identifier">set_age</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">age</span><span class="special">=</span><span class="identifier">a</span><span class="special">;</span>
        <span class="special">}</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">get_name</span><span class="special">()</span><span class="keyword">const</span>
        <span class="special">{</span>
            <span class="keyword">return</span> <span class="identifier">name</span><span class="special">;</span>
        <span class="special">}</span>

        <span class="keyword">int</span> <span class="identifier">get_age</span><span class="special">()</span><span class="keyword">const</span>
        <span class="special">{</span>
            <span class="keyword">return</span> <span class="identifier">age</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">};</span>
<span class="special">}</span>

<span class="identifier">BOOST_FUSION_ADAPT_ADT</span><span class="special">(</span>
    <span class="identifier">demo</span><span class="special">::</span><span class="identifier">employee</span><span class="special">,</span>
    <span class="special">(</span><span class="identifier">obj</span><span class="special">.</span><span class="identifier">get_name</span><span class="special">(),</span> <span class="identifier">obj</span><span class="special">.</span><span class="identifier">set_name</span><span class="special">(</span><span class="identifier">val</span><span class="special">))</span>
    <span class="special">(</span><span class="identifier">obj</span><span class="special">.</span><span class="identifier">get_age</span><span class="special">(),</span> <span class="identifier">obj</span><span class="special">.</span><span class="identifier">set_age</span><span class="special">(</span><span class="identifier">val</span><span class="special">)))</span>

<span class="identifier">demo</span><span class="special">::</span><span class="identifier">employee</span> <span class="identifier">e</span><span class="special">;</span>
<span class="identifier">front</span><span class="special">(</span><span class="identifier">e</span><span class="special">)=</span><span class="string">"Edward Norton"</span><span class="special">;</span>
<span class="identifier">back</span><span class="special">(</span><span class="identifier">e</span><span class="special">)=</span><span class="number">41</span><span class="special">;</span>
<span class="comment">//Prints 'Edward Norton is 41 years old'</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">get_name</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">get_age</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" years old"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<h5>
<a name="fusion.adapted.adapt_adt.h4"></a>
        <span class="phrase"><a name="fusion.adapted.adapt_adt.see_also"></a></span><a class="link" href="adapt_adt.html#fusion.adapted.adapt_adt.see_also">See
        also</a>
      </h5>
<p>
        <a class="link" href="../notes.html#fusion.notes.adt_attribute_proxy"><code class="computeroutput"><span class="identifier">adt_attribute_proxy</span></code></a>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2006, 2011, 2012 Joel de Guzman,
      Dan Marsden, Tobias Schwinger<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="adapt_assoc_struct_named.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../adapted.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="adapt_tpl_adt.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
